#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :demo_ecshop_backend.py
# @Time      :2021/7/29 15:30
# @author    :Harry
'''
@case : Ecshop 后台删除商品
@step：
1、进入ECShop后台登录页
2、登录后台；
3、删除商品
4、退出
'''

from selenium import webdriver
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.maximize_window()

# 1、进入ECShop后台登录页
driver.get('http://192.168.1.241/hdshop/admin/index.php')
driver.implicitly_wait(10)   # 隐式等待
driver.save_screenshot('./login.png')

# 2、登录后台；
# driver.find_element_by_name('username').send_keys('admin')
# driver.find_element_by_name('password').send_keys('hdxy2018')
# driver.find_element_by_class_name('btn-a').click()

driver.find_element(By.NAME, 'username').send_keys('admin')
driver.find_element(By.NAME, 'password').send_keys('hdxy2018')
driver.find_element(By.CLASS_NAME, 'btn-a').click()
time.sleep(2)

driver.save_screenshot('d:/backend_login_success.png')

# 3、删除商品
driver.switch_to.frame('menu-frame')  # 进入 menu frame
# driver.find_element_by_xpath('//ul[@id="menu-ul"]/li[1]').click()
driver.find_element(By.XPATH, '//ul[@id="menu-ul"]/li[1]').click()
driver.find_element_by_xpath('//ul[@id="menu-ul"]/li[1]/ul/li[1]/a').click()
driver.switch_to.default_content()   # 退出 menu frame

driver.switch_to.frame('main-frame')  # 进入main frame

wait = WebDriverWait(driver, 15, 0.5)    # 显式等待
wait.until(EC.presence_of_element_located((By.XPATH, '//div[@id="listDiv"]/table[1]/tbody/tr[3]/td[12]/a[4]/img')))
driver.find_element(By.XPATH, '//div[@id="listDiv"]/table[1]/tbody/tr[3]/td[12]/a[4]/img').click()

time.sleep(2)
driver.switch_to.alert.dismiss()  # 取消删除
time.sleep(2)
driver.find_element_by_xpath('//div[@id="listDiv"]/table[1]/tbody/tr[3]/td[12]/a[4]/img').click()
time.sleep(2)
driver.switch_to.alert.accept()  # 确定删除
driver.switch_to.default_content()    # 退出main frame

# 4、退出
time.sleep(3)     # 强制等待
driver.quit()


